
# coding: utf-8

# In[1]:

#51CTO课程频道：http://edu.51cto.com/lecturer/index/user_id-12330098.html
#优酷频道：http://i.youku.com/sdxxqbf
#微信公众号：深度学习与神经网络
#Github：https://github.com/Qinbf


# In[2]:

import numpy as np
import neurolab as nl
import matplotlib.pyplot as plt

# 0 1 2-----------16*8   
target =  np.array([[0,0,0,0,0,0,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,1,0,0,1,0,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,0,1,0,0,1,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,0,0,0,0,0,0],
           
                    [0,0,0,0,0,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,1,1,1,0,0,
                     0,0,0,0,0,0,0,0],   
           
                    [0,0,0,0,0,0,0,0,
                     0,0,1,1,1,1,0,0,
                     0,1,1,0,0,1,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,0,0,0,0,1,1,0,
                     0,0,0,0,1,1,0,0,
                     0,0,0,1,1,0,0,0,
                     0,0,1,1,0,0,0,0,
                     0,1,1,0,0,0,0,0,
                     0,1,0,0,0,0,0,0,
                     0,1,0,0,0,0,1,0,
                     0,1,0,0,0,0,1,0,
                     0,1,1,1,1,1,1,0,
                     0,0,0,0,0,0,0,0]])

#画图函数
def visualized (data, title): 
    fig, ax = plt.subplots()
    ax.imshow(data, cmap=plt.cm.gray, interpolation='nearest')
    ax.set_title(title)
    plt.show()

#显示012
for i in range(len(target)):
    visualized(np.reshape(target[i], (16,8)), i)


# In[3]:

#hopfield网络的值是1和-1
target[target == 0] = -1

#创建一个hopfield神经网络，吸引子为target(012)
net = nl.net.newhop(target)


#定义3个测试数据
test_data1 =np.asfarray([0,0,0,0,0,0,0,0,
                         0,0,0,1,1,0,1,0,
                         0,0,1,0,0,1,0,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,0,1,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,1,0,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,0,1,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,0,0,0,1,0,
                         0,1,0,1,0,0,1,0,
                         0,0,1,0,0,1,0,0,
                         0,0,1,1,1,0,0,0,
                         0,0,0,0,0,0,0,0])

test_data2 =np.asfarray([0,0,0,1,0,0,0,0,
                         0,0,0,0,1,0,0,0,
                         0,0,0,1,1,0,0,0,
                         0,0,0,0,0,0,1,0,
                         0,1,0,0,1,0,0,0,
                         0,0,0,0,1,0,0,1,
                         0,0,0,1,1,0,1,0,
                         0,1,0,0,1,0,1,0,
                         0,0,0,0,1,0,0,0,
                         0,0,1,0,1,0,1,0,
                         0,0,0,1,1,0,0,0,
                         0,0,0,0,1,0,0,0,
                         0,0,0,0,1,0,0,1,
                         0,0,1,0,1,0,0,0,
                         0,0,0,1,1,1,0,0,
                         0,1,0,0,0,0,0,0])

test_data3 =np.asfarray([0,0,0,1,0,0,0,0,
                         0,0,0,0,1,0,0,0,
                         0,0,0,1,1,0,0,0,
                         0,0,0,1,0,0,1,0,
                         0,1,0,0,0,0,0,0,
                         0,0,0,0,1,0,0,1,
                         0,0,0,1,0,0,1,0,
                         0,1,0,0,1,0,1,0,
                         0,0,0,0,1,0,0,0,
                         0,0,1,0,0,0,1,0,
                         0,0,0,1,1,0,0,0,
                         0,0,0,0,1,0,0,0,
                         0,0,0,0,0,0,0,1,
                         0,0,1,0,0,0,0,0,
                         0,0,0,0,1,1,0,0,
                         0,1,0,0,0,0,0,0])

#显示测试数据
visualized(np.reshape(test_data1, (16,8)), "test_data1")
visualized(np.reshape(test_data2, (16,8)), "test_data2")
visualized(np.reshape(test_data3, (16,8)), "test_data3")


# In[4]:

test_data1[test_data1==0] = -1
#把测试数据输入hopfield网络，得到输出
out1 = net.sim([test_data1])
#判断测试数据的数字是多少
for i in range(len(target)):
    if((out1 == target[i]).all()):
        print("test_data is :",i)
#显示输出
visualized(np.reshape(out1, (16,8)), "output1")        


test_data2[test_data2==0] = -1
#把测试数据输入hopfield网络，得到输出
out2 = net.sim([test_data2])
#判断测试数据的数字是多少
for i in range(len(target)):
    if((out2 == target[i]).all()):
        print("test_data is :",i)
#显示输出
visualized(np.reshape(out2, (16,8)), "output2")        


test_data3[test_data3==0] = -1
#把测试数据输入hopfield网络，得到输出
out3 = net.sim([test_data3])
#判断测试数据的数字是多少
for i in range(len(target)):
    if((out3 == target[i]).all()):
        print("test_data is :",i)
#显示输出
visualized(np.reshape(out3, (16,8)), "output3") 


# In[ ]:



